package com.bs.blogspace.service.impl;

import com.bs.blogspace.mapper.AddSpaceMapper;
import com.bs.blogspace.mapper.SpaceMapper;
import com.bs.blogspace.model.Result;
import com.bs.blogspace.pojo.AddSpace;
import com.bs.blogspace.pojo.Space;
import com.bs.blogspace.service.AddSpaceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

@Service
public class AddSpaceImpl implements AddSpaceService {
    @Autowired
    private SpaceMapper spaceMapper;
    @Autowired
    private AddSpaceMapper addSpaceMapper;

    @Override
    public Result<AddSpace> joinSpace(AddSpace addSpace) {
        Result<AddSpace> result = new Result<>();
        AddSpace query = addSpaceMapper.query(addSpace);
        if (query!=null){
            result.setMessage("已经加入");
            result.setCode("414");
            return result;
        }
        addSpace.setJoinTime(LocalDateTime.now());
        addSpace.setCreateTime(LocalDateTime.now());
        addSpace.setUpdateTime(LocalDateTime.now());
        //查询用户加入的空间
        Space space = spaceMapper.getBySpace(addSpace.getSpaceId());
        space.setSpacePeople(space.getSpacePeople() + 1);

        spaceMapper.updateSpacePeople(space);
        addSpaceMapper.joinSpace(addSpace);
        result.setData(addSpace);
        result.setSuccess(true);
        result.setMessage("加入空间成功");
        result.setCode("200");
        return result;
    }

    @Override
    public Result queryUserAddSpace(String phone) {
        Result<List> result = new Result<>();
        List<AddSpace> addSpace = addSpaceMapper.queryUserAddSpace(phone);
        if (addSpace.size() == 0){
            result.setCode("414");
            result.setMessage("该用户没有加入过空间");
            return result;
        }
        List<Map<String,String>> list = new ArrayList<>();
        for (int i = 0; i < addSpace.size(); i++) {
            Map<String,String> map = new HashMap<String,String>();
            map.put("spaceId", addSpace.get(i).getSpaceId());
            map.put("spaceName", addSpace.get(i).getSpaceName());
            list.add(map);
        }
        result.setData(list);
        result.setSuccess(true);
        result.setMessage("查看成功");
        result.setCode("200");
        return result;
    }

    @Override
    public Result exitSpace(AddSpace addSpace) {
        Result result = new Result();
        if (addSpace == null){
            result.setMessage("没有加入空间");
            result.setCode("414");
        }
        Space space = spaceMapper.getBySpace(addSpace.getSpaceId());
        boolean isExit = addSpaceMapper.exitSpace(addSpace);
         if (isExit){
             space.setSpacePeople(space.getSpacePeople() - 1);
             spaceMapper.updateSpacePeople(space);
             result.setMessage("退出空间成功");
             result.setCode("200");
             result.setSuccess(true);
         }
        return result;
    }
}
